import copy

import pandas as pd

import config


def class_time(data):
    new_data = []
    for item in data:
        index = item[0]
        subject = item[1]
        subjectname = item[2]
        teacherID = item[3]
        teacher = item[4]
        classes = item[5]
        studentID = item[6]
        wenkstart = item[7]
        wenkend = item[8]
        studyhour = item[9]
        hours = item[10]
        weektype = item[11]
        times = item[12:]

        for time in times:
            new_item = [index, subject, subjectname, teacherID, teacher, classes, studentID, wenkstart, wenkend,
                        studyhour, hours, weektype, time]
            new_data.append(new_item)

    week_days = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    new_datas = []
    for item in new_data:
        room_info, time_info = item[-1]
        classroom = room_info
        time_number = (time_info - 1) % (config.periods_per_day) + 1  # 计算时间号对应第几节课
        week_day = week_days[(time_info - 1) // (config.periods_per_day)]  # 计算时间号对应第几周
        new_item = item[:12] + [classroom, (week_day, time_number)]
        new_datas.append(new_item)

    return new_datas


# [[Decimal('1'), '00121009', '聚合原理', '4566', '沈春晖', '1', ['1049722000273'], 1, 16, 2, 36.('马院104', 50), ('周日', 3)],

def save_scheme(SCH):
    scheme = copy.deepcopy(SCH)
    scheme_data = class_time(scheme)
    column_names = ['教学任务号', '课程号', '课程名', '老师号', '老师名', '班级', '学生号', '开始周', '结束周', '周课时', '总课时', '周类型', '教室', '时间']
    df = pd.DataFrame()
    for i, col_name in enumerate(column_names):
        df[col_name] = [row[i] for row in scheme_data]
    file_path = 'data/scheme.xlsx'  # 指定文件路径和名称
    sheet_name = 'Sheet1'  # 工作表名称

    df.to_excel(file_path, sheet_name=sheet_name, index=False)


def save_data(exp_data):
    column_names=['狮群','迭代','步长因子', '时间','满意度','空闲率','距离','目标函数']
    df = pd.DataFrame()
    for i,column_name in enumerate(column_names):
        df[column_name] =[row[i] for row in exp_data]
    file_path = 'data/scheme1.xlsx'
    sheet_name = 'Sheet1'

    df.to_excel(file_path, sheet_name=sheet_name, index=False)